﻿using System.Text;

namespace CodeParser.Tokens
{
    public class Float
        : TokenParser
    {

        public override Token GetTokenAt(string source, int index)
        {
            var point = 0;
            var i = index;
            var sourceLength = source.Length;
            var token = new StringBuilder();
            var numberAfterPoint = false;
            char currentChar = ' ';
            while (i < sourceLength)
            {
                currentChar = source[i++];
                if (currentChar == '.')
                {
                    point++;
                    if (point == 1)
                    {
                        token.Append('.');
                        continue;
                    }
                    break;
                }
                if (currentChar < '0' || currentChar > '9')
                {
                    break;
                }
                if (point > 0) numberAfterPoint = true;
                token.Append(currentChar);
            }
            if (numberAfterPoint || (point == 0 && token.Length > 0))
            {
                if (currentChar == 'f' || currentChar == 'l' || currentChar == 'h')
                    token.Append(currentChar);
                return new Token() { Index = index, Type = TokenType.LiteralFloat, Value = token.ToString() };
            }
            return null;
        }

    }
}
